package com.alex.alexelasticsearch.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.*;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;

/**
 * 电商商品实体类 - 优化版本
 * 支持ES 8.x，解决类型映射问题
 */
@Document(indexName = "alex008_products")
public class Alex008Product {
    
    @Id
    private String id;
    
    // 使用IK分词器进行中文分词
    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
    private String productName;
    
    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
    private String description;
    
    // 品牌支持精确匹配和模糊搜索
    @MultiField(
        mainField = @Field(type = FieldType.Text, analyzer = "ik_smart"),
        otherFields = {
            @InnerField(suffix = "keyword", type = FieldType.Keyword)
        }
    )
    private String brand;
    
    @Field(type = FieldType.Keyword)
    private String category;
    
    // BigDecimal映射为scaled_float，提高精度
    @Field(type = FieldType.Scaled_Float, scalingFactor = 100)
    private BigDecimal price;
    
    @Field(type = FieldType.Long)
    private Long salesCount;
    
    @Field(type = FieldType.Double)
    private Double rating;
    
    @Field(type = FieldType.Long)
    private Long stock;

    // 修复时间字段类型不匹配问题
    @Field(type = FieldType.Keyword)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private String createTime;
    
    @Field(type = FieldType.Keyword)
    private String[] tags;
    
    // 新增字段：商品状态
    @Field(type = FieldType.Keyword)
    private String status = "ACTIVE"; // ACTIVE, INACTIVE, OUT_OF_STOCK
    
    // 新增字段：是否推荐
    @Field(type = FieldType.Boolean)
    private Boolean isRecommended = false;

    // 构造函数
    public Alex008Product() {}
    
    public Alex008Product(String productName, String description, String brand, 
                         String category, BigDecimal price, Long salesCount, 
                         Double rating, Long stock, String... tags) {
        this.productName = productName;
        this.description = description;
        this.brand = brand;
        this.category = category;
        this.price = price;
        this.salesCount = salesCount;
        this.rating = rating;
        this.stock = stock;
        this.createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        this.tags = tags;
        this.status = "ACTIVE";
        this.isRecommended = false;
    }
    
    // 便捷构造函数 - 用于快速创建推荐商品
    public Alex008Product(String productName, String description, String brand, 
                         String category, BigDecimal price, Long salesCount, 
                         Double rating, Long stock, Boolean isRecommended, String... tags) {
        this(productName, description, brand, category, price, salesCount, rating, stock, tags);
        this.isRecommended = isRecommended;
    }

    // 业务方法
    /**
     * 判断是否为热销商品
     */
    public boolean isHotSale() {
        return this.salesCount != null && this.salesCount > 10000L;
    }
    
    /**
     * 判断是否为高评分商品
     */
    public boolean isHighRated() {
        return this.rating != null && this.rating >= 4.5;
    }
    
    /**
     * 判断是否有库存
     */
    public boolean isInStock() {
        return this.stock != null && this.stock > 0;
    }
    
    /**
     * 更新库存
     */
    public void updateStock(Long newStock) {
        this.stock = newStock;
        if (newStock <= 0) {
            this.status = "OUT_OF_STOCK";
        } else if ("OUT_OF_STOCK".equals(this.status)) {
            this.status = "ACTIVE";
        }
    }

    // Getter和Setter方法
    public String getId() { return id; }
    public void setId(String id) { this.id = id; }
    
    public String getProductName() { return productName; }
    public void setProductName(String productName) { this.productName = productName; }
    
    public String getDescription() { return description; }
    public void setDescription(String description) { this.description = description; }
    
    public String getBrand() { return brand; }
    public void setBrand(String brand) { this.brand = brand; }
    
    public String getCategory() { return category; }
    public void setCategory(String category) { this.category = category; }
    
    public BigDecimal getPrice() { return price; }
    public void setPrice(BigDecimal price) { this.price = price; }
    
    public Long getSalesCount() { return salesCount; }
    public void setSalesCount(Long salesCount) { this.salesCount = salesCount; }
    
    public Double getRating() { return rating; }
    public void setRating(Double rating) { this.rating = rating; }
    
    public Long getStock() { return stock; }
    public void setStock(Long stock) { this.stock = stock; }
    
    public String getCreateTime() { return createTime; }
    public void setCreateTime(String createTime) { this.createTime = createTime; }
    
    // 便捷方法：设置LocalDateTime并自动转换为String
    public void setCreateTimeFromLocalDateTime(LocalDateTime dateTime) {
        if (dateTime != null) {
            this.createTime = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }
    }
    
    // 便捷方法：获取LocalDateTime
    public LocalDateTime getCreateTimeAsLocalDateTime() {
        if (createTime != null) {
            return LocalDateTime.parse(createTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }
        return null;
    }
    
    public String[] getTags() { return tags; }
    public void setTags(String[] tags) { this.tags = tags; }
    
    public String getStatus() { return status; }
    public void setStatus(String status) { this.status = status; }
    
    public Boolean getIsRecommended() { return isRecommended; }
    public void setIsRecommended(Boolean isRecommended) { this.isRecommended = isRecommended; }
    
    @Override
    public String toString() {
        return "Alex008Product{" +
                "id='" + id + '\'' +
                ", productName='" + productName + '\'' +
                ", brand='" + brand + '\'' +
                ", category='" + category + '\'' +
                ", price=" + price +
                ", salesCount=" + salesCount +
                ", rating=" + rating +
                ", stock=" + stock +
                ", createTime='" + createTime + '\'' +
                ", status='" + status + '\'' +
                ", isRecommended=" + isRecommended +
                ", tags=" + Arrays.toString(tags) +
                '}';
    }
    
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Alex008Product that = (Alex008Product) o;
        return id != null ? id.equals(that.id) : that.id == null;
    }
    
    @Override
    public int hashCode() {
        return id != null ? id.hashCode() : 0;
    }
}